package cpoo.video;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.io.IOException;

import javax.media.CannotRealizeException;
import javax.media.Codec;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JComponent;

import cpoo.exceptions.MotionException;

public class VideoManager {
	private JComponent videoPanel;
	private JComponent controlerPanel;
	
	private Component videoComponent;
	private Component controlerComponent;
	
	private File movie = null;
	private Player player;
	private PreProcessor processor;
	
	public VideoManager (JComponent videoPanelRef, JComponent controlerPanelRef){
		this.videoPanel = videoPanelRef;
		this.controlerPanel = controlerPanelRef;
		if (videoPanel != null){
			videoPanel.addComponentListener(new ComponentListener() {
				@Override
				public void componentShown(ComponentEvent e) {
					
				}
				@Override
				public void componentResized(ComponentEvent e) {
					if (videoComponent != null){
						videoComponent.setBounds(0, 0, videoPanel.getWidth(), videoPanel.getHeight());
					}
				}
				@Override
				public void componentMoved(ComponentEvent e) {
					
				}
				@Override
				public void componentHidden(ComponentEvent e) {
					
				}
			});
		}
		if (controlerPanel != null){
			controlerPanel.addComponentListener(new ComponentListener() {
				@Override
				public void componentShown(ComponentEvent e) {
					
				}
				@Override
				public void componentResized(ComponentEvent e) {
					if (controlerComponent != null){
						controlerComponent.setBounds(0, 0, controlerPanel.getWidth(), controlerPanel.getHeight());
					}
				}
				@Override
				public void componentMoved(ComponentEvent e) {
					
				}
				@Override
				public void componentHidden(ComponentEvent e) {
					
				}
			});
		}
	}
	
	public void addCodec(Codec c){
		processor.addCodec(c);
	}
	
	public void clearCodecs(){
		processor.clearCodecs();
	}
	
	public Dimension getVideoSize(){
		return processor.getVideoSize();
	}
	
	public void insertMovie(File input) throws MotionException{
		movie = input;
		
		//MediaLocator mediaLoc = new MediaLocator("file:///" + movie);
		if (processor != null){
			processor.stop();
		}
		if (player != null){
			player.stop();
			player.close();
		}
//		try {
			processor = new PreProcessor(movie);
			
			//player = Manager.createRealizedPlayer(mediaLoc);
//			player = Manager.createRealizedPlayer(processor.getProcessorOutput());
//			
//			if (videoPanel != null){
//				videoComponent = player.getVisualComponent();
//				videoComponent.setBounds(0,0,videoPanel.getWidth(),videoPanel.getHeight());
//				videoPanel.removeAll();
//				videoPanel.add(videoComponent);
//			}
//			if (controlerPanel != null){
//				controlerComponent = player.getControlPanelComponent();
//				controlerComponent.setBounds(0,0,controlerPanel.getWidth(),controlerPanel.getHeight());
//				controlerPanel.removeAll();
//				controlerPanel.add(controlerComponent);
//			}
//		} catch (MotionException e) {
//			e.printInfo();
//			e.printStackTrace();
//		}
	}
	
	public void play() throws MotionException{
		if (processor != null){
			processor.play();
			try {
				player = Manager.createRealizedPlayer(processor.getProcessorOutput());
				
				if (videoPanel != null){
					videoComponent = player.getVisualComponent();
					videoComponent.setBounds(0,0,videoPanel.getWidth(),videoPanel.getHeight());
					videoPanel.removeAll();
					videoPanel.add(videoComponent);
				}
				if (controlerPanel != null){
					controlerComponent = player.getControlPanelComponent();
					controlerComponent.setBounds(0,0,controlerPanel.getWidth(),controlerPanel.getHeight());
					controlerPanel.removeAll();
					controlerPanel.add(controlerComponent);
				}
				player.start();
			} catch (CannotRealizeException me){
				me.printStackTrace();
			}  catch (NoPlayerException me){
				me.printStackTrace();
			}  catch (IOException me){
				me.printStackTrace();
			}
		}
	}
	
	public void eject(){
		if (processor != null){
			processor.stop();
		}
		if (player != null){
			player.stop();
			player.close();
		}
	}
}
